


$(document).ready(function() {
	validateForm($('#stallsAdd'));
	addCustomRule();
	initRules();
	$(".location").click(function(){		
		var $parent = $(this).closest("div.location_info");
		if($parent.hasClass("clicked")){
			$parent.removeClass("clicked");
			$parent.find(".stalls_row").removeClass("hide");
			$parent.removeClass("clicked");
			$(this).html('<span class="glyphicon glyphicon-minus-sign"></span>');
			
			num = $(this).attr("class").replace(/[^0-9]/g,'');
			initRules(num);
			
		}else{
			$parent.addClass("clicked");
			$parent.find(".stalls_row").addClass("hide");
			$parent.addClass("clicked");
			$(this).html('<span class="glyphicon glyphicon-plus-sign"></span>');
			$parent.find("input[type='text']").val("");
			$parent.find("input[type='text']").rules("remove");
		}
	});
});


function validateForm($form){
	$form.validate({
        onfocusout : function( element, event ) {
		    if ($(element).valid()){
				$(element).closest('div.stalls_row').find('.category_group_warntip').removeClass("show");
	    		$(element).closest('div.stalls_row').find('.category_group_warntip').addClass("hide");
		    }
		},
        onkeyup: function( element, event ) {
		    $(element).valid();
		},
		
        errorPlacement: function (error, element) {
        	if($(element).hasClass('postImage')){
        		$('.images-tip-error').html("chọn ít nhất 1 hình");
        	}else{
	            $(element).closest('div.stalls_row').find('.category_group_warntip').removeClass("hide");
	            $(element).closest('div.stalls_row').find('.category_group_warntip').addClass("show");
            }
        },
        invalidHandler: function(form, validator) {
            if (!validator.numberOfInvalids()){
                return;
            }
            scrollToTopElement($(validator.errorList[0].element));
        }
    });
    
}
function initRules(){
	$('#data_category_id').rules("add", {
	        validateSelect : "0"
	});
	$('#data_shop_name').rules("add", {
            required : true
      });
 	$('#data_address').rules("add", {
        required : true
    });
    $("#data_phone_number").rules("add", {
            required : true,
            number: true
    });
    $("#data_email").rules("add", {
            required : true,
            email: true
    });
    
    $('#data_website').rules("add", {
            required : true,
            validateDomainName: true
    });
    if($("#shop_id").val() == undefined){
    	
	    $("#data_banner_image").rules("add", {
	            required : true,
	            extension: "png|jpg|jpeg|gif"
	    });
	    $("#data_logo_image").rules("add", {
	            required : true,
	            extension: "png|jpg|jpeg|gif"
	    });
    }
    // location 0
    initRuleLocation(0);
    initRuleLocation(1);
    initRuleLocation(2);
}
function initRuleLocation(locationNum){
	$("#data_lc_phone" + locationNum).rules("add", {
            required : true,
            number: true
    });
    
    $("#data_lc_name" + locationNum).rules("add", {
            required : true,
    });
    
    $("#data_lc_fax" + locationNum).rules("add", {
            required : false,
            number: true
    });
    
    $("#data_lc_email" + locationNum).rules("add", {
            required : true,
            email: true
    });
    
    $("#data_lc_facebook" + locationNum).rules("add", {
            required : false
    });
}
function scrollToTopElement($element){
    $('html, body').animate({
        scrollTop: $element.offset().top
    }, 100);
}

/**
 * add rule
 */
 function addCustomRule(){
 	 $.validator.addMethod('validateDomainName', function (value, element, arg) {
       return /^www\.[a-z0-9](?:[a-z0-9-]?[a-z0-9])*\.[a-z]{2,6}$/.test(value);
    }, 'Nhập địa chỉ trang web chưa chính xác');
    
   $.validator.addMethod('validateSelect', function (value, element, arg) {
        return arg != value;
    }, 'Vui lòng chọn');
 }
